package com.eascs.web.o2o.scheduler.history;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.web.o2o.scheduler.entity.O2oPaymentDetailEntity;

@Service
public class PaymentDetailService {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	BaseService baseService;
	

	public void savePaymentDetail() {
		String sql="";
		//1.同步老系统的保证金、管理费、风向补偿金收取明
		logger.info("同步老系统的保证金、管理费、风向补偿金收取明细开始......");
		try {
			 sql="SELECT l.loanapplyno AS loanapply_no, l.billcode AS bill_code, 'S' AS payment_type, 'BZJ' AS subject_type, l.existdeposit AS amount_money, t.update_time, l.creator AS operation_no, l.creatorname operation_name, 0 AS happen_source FROM ( SELECT loanapplyno, max(wf.auditordate) AS update_time FROM eadb.o2o_loanapply l LEFT JOIN eadb.wf_processtaskhistory wf ON l.loanapplyno = wf.billno WHERE l.islock = 'N' AND wf.billtype = 'STZ' AND l.billstatusinform = 'CLO' AND l.loanapplyno_main IS NULL GROUP BY loanapplyno ) t LEFT JOIN eadb.o2o_loanapply l ON t.loanapplyno = l.loanapplyno WHERE existdeposit > 0 UNION ALL ( SELECT l.loanapplyno AS loanapply_no, l.billcode AS bill_code, 'S' AS payment_type, 'GLF' AS subject_type, l.managefee AS amount_money, t.update_time, l.creator AS operation_no, l.creatorname operation_name, 0 AS happen_source FROM ( SELECT loanapplyno, max(wf.auditordate) AS update_time FROM eadb.o2o_loanapply l LEFT JOIN eadb.wf_processtaskhistory wf ON l.loanapplyno = wf.billno WHERE l.islock = 'N' AND wf.billtype = 'STZ' AND l.billstatusinform = 'CLO' AND l.loanapplyno_main IS NULL GROUP BY loanapplyno ) t LEFT JOIN eadb.o2o_loanapply l ON t.loanapplyno = l.loanapplyno WHERE managefee > 0 ) UNION ALL ( SELECT l.loanapplyno AS loanapply_no, l.billcode AS bill_code, 'S' AS payment_type, 'FXBCJ' AS subject_type, l.existriskdeposit AS amount_money, t.update_time, l.creator AS operation_no, l.creatorname operation_name, 0 AS happen_source FROM ( SELECT loanapplyno, max(wf.auditordate) AS update_time FROM eadb.o2o_loanapply l LEFT JOIN eadb.wf_processtaskhistory wf ON l.loanapplyno = wf.billno WHERE l.islock = 'N' AND wf.billtype = 'STZ' AND l.billstatusinform = 'CLO' AND l.loanapplyno_main IS NULL GROUP BY loanapplyno ) t LEFT JOIN eadb.o2o_loanapply l ON t.loanapplyno = l.loanapplyno WHERE existriskdeposit > 0 )";
			baseService.loadData(sql, O2oPaymentDetailEntity.class);
		} catch (Exception e) {
			logger.error("同步老系统的保证金、管理费、风向补偿金收取明出现异常......");
			throw e;

		}
		logger.info("同步老系统的保证金、管理费、风向补偿金收取明结束......");
		//2.同步老系统的保证金、管理费支出明细
		logger.info("同步老系统的保证金、管理费支出明细开始......");
		try {
			 sql="SELECT l.loanapplyno AS loanapply_no, l.billcode AS bill_code, 'F' AS payment_type, 'BZJ' AS subject_type, l.undeposit AS amount_money, t.update_time, l.creator AS operation_no, l.creatorname operation_name, 0 AS happen_source FROM ( SELECT l.loanapplyno, max(wf.auditordate) AS update_time FROM eadb.o2o_backdepositfee l LEFT JOIN eadb.wf_processtaskhistory wf ON l.backdepositfeeno = wf.billno WHERE l.islock = 'N' AND wf.billtype = 'TFO' AND l.billstatus = 'CLO' GROUP BY loanapplyno ) t LEFT JOIN eadb.o2o_loanapply l ON t.loanapplyno = l.loanapplyno WHERE undeposit > 0 UNION ALL ( SELECT l.loanapplyno AS loanapply_no, l.billcode AS bill_code, 'F' AS payment_type, 'GLF' AS subject_type, l.existmanagefee AS amount_money, t.update_time, l.creator AS operation_no, l.creatorname operation_name, 0 AS happen_source FROM ( SELECT f.loanapplyno, max(wf.auditordate) AS update_time FROM eadb.o2o_backmanagefee f LEFT JOIN eadb.wf_processtaskhistory wf ON f.backmanagefeeno = wf.billno WHERE f.islock = 'N' AND wf.billtype = 'TFG' AND f.billstatus = 'CLO' GROUP BY loanapplyno ) t LEFT JOIN eadb.o2o_loanapply l ON t.loanapplyno = l.loanapplyno WHERE existmanagefee > 0 )";
			baseService.loadData(sql, O2oPaymentDetailEntity.class);
		} catch (Exception e) {
			logger.error("同步老系统的保证金、管理费支出明细出现异常......");
			throw e;

		}
		logger.info("同步老系统的保证金、管理费支出明细结束......");
		
		// 2.同步新系统的财务明细
		logger.info("同步新系统的财务明细开始......");
		try {
			sql = "select loanapply_no,bill_code,payment_type,subject_type,amount_money,update_time,operation_no,operation_name,1 as happen_source from o2o_payment_detail";
			baseService.loadServiceData(sql, O2oPaymentDetailEntity.class, "");
		} catch (Exception e) {
			logger.error("同步新系统的财务明细出现异常......");
			throw e;

		}
		logger.info("同步新系统的财务明细结束......");
		
	}
	
	
	
}
